﻿using AutoMapper;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using WMS.FC.Domain.DomainObject.System;
using WMS.FC.Domain.PersistenceObject.Business;
using WMS.FC.Domain.Repository;
using WMS.FC.Domain.Service.System;
using WMS.FC.Domain.Service.Tray.Interface;

namespace WMS.FC.Domain.Service.Tray
{
    public class PileTrayService : ServiceBase, IPileTrayService
    {
        protected readonly ITrayRepository trayRepository;
        protected readonly ICheckTrayStatusService checkTrayStatusService;

        public PileTrayService(
            ILogger<ServiceBase> logger,
            IOptions<WmsSettings> options,
            IMapper mapper,
            ITrayRepository trayRepository,
            ICheckTrayStatusService checkTrayStatusService)
            : base(logger, options, mapper)
        {
            this.trayRepository = trayRepository;
            this.checkTrayStatusService = checkTrayStatusService;
        }

        public async Task<ActionResult<TrayPO>> PileTray(string? trayBarcode1, string? trayBarcode2, string? trayBarcode3, UserInfo userInfo)
        {
            var result = new ActionResult<TrayPO>("托盘叠盘");
            try
            {
                if (string.IsNullOrWhiteSpace(trayBarcode1))
                    return result.ArgumentError("托盘条码不能为空");

                if (string.IsNullOrWhiteSpace(trayBarcode2) && !string.IsNullOrWhiteSpace(trayBarcode3))
                    return result.ArgumentError($"托盘条码参数异常,底层条码:({trayBarcode1}),中层条码:({trayBarcode2}),上层条码:({trayBarcode3})");

                var trayPOList = await trayRepository.GetTrayListByBarcode(trayBarcode1);
                if (string.IsNullOrWhiteSpace(trayBarcode2))
                {
                    if (trayPOList.Count == 1)
                    {
                        result.Data = trayPOList[0];
                        return result.PartSuccess("托盘组条码与扫码一致");
                    }
                }
                else
                {
                    if (string.IsNullOrWhiteSpace(trayBarcode3) && trayPOList.Count == 2 && trayPOList[1].Barcode == trayBarcode2 && trayPOList[1].TrayPosition == 2)
                    {
                        result.Data = trayPOList[0];
                        return result.PartSuccess("托盘组条码与扫码一致");
                    }
                    else if (!string.IsNullOrWhiteSpace(trayBarcode3) && trayPOList.Count == 3 && trayPOList[1].Barcode == trayBarcode2 && trayPOList[1].TrayPosition == 2 && trayPOList[2].Barcode == trayBarcode3 && trayPOList[2].TrayPosition == 3)
                    {
                        result.Data = trayPOList[0];
                        return result.PartSuccess("托盘组条码与扫码一致");
                    }
                }

                var unpileResult = await UnpileTray(trayBarcode1, userInfo);
                if (!unpileResult.IsSuccess)
                    return result.Fail(unpileResult);

                trayPOList.Add(unpileResult.Data!);

                if (!string.IsNullOrWhiteSpace(trayBarcode2))
                {
                    unpileResult = await UnpileTray(trayBarcode2, userInfo);
                    if (!unpileResult.IsSuccess)
                        return result.Fail(unpileResult);

                    if (trayPOList[0].MaterialCode != unpileResult.Data!.MaterialCode)
                        return result.BusinessError($"托盘条码:({trayBarcode1}),托盘条码:({trayBarcode2}),类型不一致");

                    if (trayPOList[0].TechnologyRouteCode != unpileResult.Data!.TechnologyRouteCode || trayPOList[0].TechnologyProcessCode != unpileResult.Data!.TechnologyProcessCode)
                        return result.BusinessError($"托盘条码:({trayBarcode1}),托盘条码:({trayBarcode2}),工艺不一致");

                    trayPOList.Add(unpileResult.Data!);
                }

                if (!string.IsNullOrWhiteSpace(trayBarcode3))
                {
                    unpileResult = await UnpileTray(trayBarcode3, userInfo);
                    if (!unpileResult.IsSuccess)
                        return result.Fail(unpileResult);

                    if (trayPOList[0].MaterialCode != unpileResult.Data!.MaterialCode)
                        return result.BusinessError($"托盘条码:({trayBarcode1}),托盘条码:({trayBarcode3}),类型不一致");

                    if (trayPOList[0].TechnologyRouteCode != unpileResult.Data!.TechnologyRouteCode || trayPOList[0].TechnologyProcessCode != unpileResult.Data!.TechnologyProcessCode)
                        return result.BusinessError($"托盘条码:({trayBarcode1}),托盘条码:({trayBarcode3}),工艺不一致");

                    trayPOList.Add(unpileResult.Data!);
                }

                for (var i = 0; i < trayPOList.Count; i++)
                {
                    trayPOList[i].ContainerBarcode = trayBarcode1;
                    trayPOList[i].TrayPosition = i + 1;
                }

                var count = await trayRepository.Update(trayPOList, userInfo);
                if (count == 0)
                    return result.DbError("更新托盘数据异常");

                result.Data = trayPOList[0];
            }
            catch (Exception ex)
            {
                return result.Exception(ex);
            }

            return result;
        }

        public async Task<ActionResult<TrayPO>> UnpileTray(string trayBarcode, UserInfo userInfo)
        {
            var result = new ActionResult<TrayPO>("托盘拆叠盘");
            try
            {
                var checkTrayStatusResult = await checkTrayStatusService.CheckTrayStatus(trayBarcode);
                if (!checkTrayStatusResult.IsSuccess)
                    return result.Fail(checkTrayStatusResult);

                var trayPO = checkTrayStatusResult.Data!;
                var count = await trayRepository.UnpileTray(trayPO.ContainerBarcode, userInfo);
                if (count == 0)
                    return result.DbError($"托盘条码:{trayBarcode},拆叠盘异常");

                result.Data = trayPO;
            }
            catch (Exception ex)
            {
                return result.Exception(ex);
            }

            return result;
        }
    }
}
